<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="资产分类" prop="parentCode">
        <TreeSelect
          v-model="formItem.parentCode"
          :multiple="false"
          :options="typeOptions"
          placeholder="选择资产分类"
        />
      </el-form-item>
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入分类名称" />
      </el-form-item>
      <el-form-item label="分类编码" prop="code">
        <el-input v-model="formItem.code" placeholder="请输入分类编码" />
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-input v-model="formItem.unit" placeholder="请输入计量单位" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  update,
  detail
} from '@neu/dcam-ui/api/spareType'
import TreeSelect from '@neu/biz-core/components/TreeSelect'
import {
  tree
} from '@neu/dcam-ui/api/assetType'

// 定义一个表单原始项
const formItem = {
  name: '',
  code: '',
  sort: '',
  unit: '',
  parentCode: null
}
export default {
  name: 'FixedSpareTypeEditForm',
  components: {
    TreeSelect
  },
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        parentCode: [
          { required: true, message: '选择资产分类', trigger: 'change' }
        ],
        name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '类型编码不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        unit: [
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ]
      },
      modal: true,
      /** 类型树 */
      typeOptions: []
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
    this.assetTypeTree()
  },
  methods: {
    // 类型树
    assetTypeTree() {
      tree().then(response => {
        this.typeOptions = response.data
      })
    },
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    edit() {
      this.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
      })
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
